// shopPages/delstaff/delstaff.js

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    position: '',
    staffid: '',
    imgsrc: '',
    kehunum: '',
    list: [],
    numArr: [],
    idArr: [],
    total: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      position: options.position,
      staffid: options.staffid,
      name: decodeURIComponent(options.staffName),
      type: options.type,
      imgsrc: decodeURIComponent(options.imgsrc)
    });
    this.getnew()
  },
  // 减
  bindjian(e) {
    var that = this
    var itemNum = e.currentTarget.dataset.num
    var itemindex = e.currentTarget.dataset.index
    if (itemNum != 0) {
      var num = Number(itemNum) - 1
    } else {
      var num = 0
    }

    var newarr = that.data.numArr
    for (var i = 0; i < newarr.length; i++) {
      if (i == itemindex) {
        newarr[itemindex] = num
      }
    }
    var sum = that.data.numArr
    var add = function (a, b) {
      return a + b;
    }
    var total = sum.reduce(add, 0)
    that.setData({
      total: total,
      numArr: newarr
    })
  },
  // 加
  bindjia(e) {
    var that = this
    var itemNum = e.currentTarget.dataset.num
    var itemindex = e.currentTarget.dataset.index
    var num = Number(itemNum) + 1
    var newarr = that.data.numArr
    for (var i = 0; i < newarr.length; i++) {
      if (i == itemindex) {
        newarr[itemindex] = num
      }
    }
    var sum = that.data.numArr
    var add = function (a, b) {
      return a + b;
    }
    var total = sum.reduce(add, 0)
    that.setData({
      total: total,
      numArr: newarr
    })
  },
  bindunput(e) {
    var that = this
    var itemindex = e.currentTarget.dataset.index
    var newarr = that.data.numArr
    for (var i = 0; i < newarr.length; i++) {
      if (i == itemindex) {
        newarr[itemindex] = Number(e.detail.value)
      }
    }
    var sum = that.data.numArr
    var add = function (a, b) {
      return a + b;
    }
    var total = sum.reduce(add, 0)
    that.setData({
      total: total,
      numArr: newarr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // },
  getnew() {
    var that = this
    wx.request({
      url: app.globalData.bossradar + '/api/1.0/getDelStaffInfo',
      data: {
        staffId: that.data.staffid
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        var numArr = []
        var idArr = []
        for (var i = 0; i < res.data.data.list.length; i++) {
          numArr.push(res.data.data.list[i].num)
          idArr.push(res.data.data.list[i].staffId)
        }
        that.setData({
          kehunum: res.data.data.delStaffInfoDto.customerCount,
          list: res.data.data.list,
          numArr: numArr,
          idArr: idArr
        })
      }
    })
  },

  delMember: function () {

    var that = this;
    var total = that.data.total;
    var numarr = that.data.numArr;
    var idarr = that.data.idArr;
    var listarr = []
    for (var i = 0; i < numarr.length; i++) {
      var obj = {}
      obj.staffId = idarr[i]
      obj.count = numarr[i]
      if (obj.count > 0) {
        listarr.push(obj)
      }
    }
    if (that.data.staffid == app.globalData.userInfo.id) {
      wx.showToast({
        title: '管理员不能被删除',
        icon: 'none'
      })
    } else {
      if (total != that.data.kehunum) {
        wx.showToast({
          title: '分配数大于或小于客户数',
          icon: 'none'
        })

      } else {
        wx.showModal({
          title: '提示',
          content: '确定删除吗?',
          success(res) {
            if (res.confirm) {

              wx.request({
                url: app.globalData.bossradar + '/api/1.0/distribute',
                data: {
                  originalStaffId: that.data.staffid,
                  list: listarr
                },
                method: 'POST',
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success(res) {
                  console.log('成功')
                  that.setData({
                    staffList: res.data.data
                  })
                  wx.navigateBack({})
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    }
  },
  delpartner: function () {
    var that = this;
    console.log("========================================");
    if (that.data.staffid == app.globalData.userInfo.id) {
      wx.showToast({
        title: '管理员不能被删除',
        icon: 'none'
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '确定删除吗?',
        success(res) {
          if (res.confirm) {
            wx.request({
              url: app.globalData.bossradar + '/api/1.0/deletePartner',
              data: {
                bossId: app.globalData.userInfo.id,
                uid: that.data.staffid
              },
              method: 'POST',
              header: {
                'content-type': 'application/json' // 默认值
              },
              success(res) {
                console.log(res.data)
                that.setData({
                  hehuoList: res.data.data
                })
                wx.navigateBack({})
              }
            })

          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })

    }
  }
})